package com.manager.system.service.impl;
import com.manager.common.dao.SuperDao;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.model.File_Entity;
import com.manager.system.service.FileService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.util.*;

/**
 * 文件管理
 * @author zlj
 *
 */
@SuppressWarnings("all")
@Service
@Transactional
public class FileServiceImpl extends SuperDao<Object> implements FileService{

	@Override
	public PageVO getFilePage(File_Entity model) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer();
		sb.append(" FROM File_Entity t WHERE 1 = 1 AND t.fileStatus = 0 ");
		if(StringUtils.isNotEmpty(model.getFileName())){
			sb.append(" and t.fileName like # ");
			params.add("%"+model.getFileName()+"%");
		}
		if(model.getBusinessType() != null){
			sb.append(" and t.businessType = # ");
			params.add(model.getBusinessType());
		}
		if(StringUtils.isNotEmpty(model.getOtherType())){
			sb.append(" and t.otherType = # ");
			params.add(model.getOtherType());
		}
		String sql = sb.toString();
		sql = ObjectUtils.replaceIndexByList(sql, params);
		List<File_Entity> list = this.getList("select t " + sql,params.toArray(), model.getStart(), model.getLimit(), " order by t.createTime desc ");
		int sum = this.getListCount("select count(*) "+ sql, params.toArray(), "");
		int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));

		return PageVO.create(model.getStart(),count, ObjectUtils.getJSONArray(File_Entity.config,list));
	}

	@Override
	public void addFile(File_Entity model) {
		this.addT(model);
	}


	@Override
	public void deleteFile(File_Entity model) {
		File_Entity file_entity = this.getT(model.getId(),File_Entity.class);
		file_entity.setFileStatus(1);
		this.updateT(file_entity);
	}


	@Override
	public File_Entity getFileEntityById(String id) {
		File_Entity fileEntity = this.getT(id,File_Entity.class);
		return fileEntity;
	}

	@Override
	public void addBatchFile(List<File_Entity> fileEntityList) throws Exception {
		this.batchAddT(fileEntityList);
	}

	@Override
	public void updateFileConvertFilePath(File_Entity model) {
		File_Entity fileEntity = this.getT(model.getId(),File_Entity.class);
		//fileEntity.setConvertFilePath(model.getConvertFilePath());
		BeanUtils.copyProperties(model,fileEntity,new String[]{"id"});
		this.updateT(fileEntity);
	}
}
